package com.dchh.android.suverynew.act;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dhcc.android.base.gis.FactoryGPS;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

import java.io.IOException;

import srs.GPS.GPSConvert;
import srs.Geometry.IEnvelope;
import srs.Geometry.IPoint;
import srs.Geometry.Point;
import srs.tools.MapControl;
import srs.tools.ZoomInCommand;
import srs.tools.ZoomOutCommand;

/**
 * 直线导航 add 杨宗仁
 *
 * @author Tudou
 */
public class NaviLineActivity extends SBaseActivity {
    /**
     * 遥感导航地图控件
     */
    MapControl mNaviMapControl;
    // */布局控件
    private TextView mtvInfoDirectory = null;
    private TextView mtvInfoDistance = null;
    private TextView mtvInfoSpeed = null;
    private TextView mInfoo = null;
    private ImageView mivZoomin;
    private ImageView mivZoomout;
    private ImageView mivLocationCenter;
    private View main;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_navi_naviline);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("直线导航");
    }

    /**
     * 初始化控件并设置监听
     */
    @Override
    public void initView() {
        init();
        /* 获取地图显示范围 */
        IEnvelope envMapOld = CommenUtil.getMap().getExtent();
        mNaviMapControl.setMap(CommenUtil.getMap());
        // 设置GPS信息显示的背景色
        mInfoo.setBackgroundColor(Color.argb(128, 255, 255, 255));
        // 设置GPS信息显示的文本颜色
        mInfoo.setTextColor(Color.BLACK);
        // 显示GPS信息
        try {
            FactoryGPS factory = new FactoryGPS(mInfoo, mtvInfoDirectory,
                    mtvInfoDistance, mtvInfoSpeed, mNaviMapControl);
            Log.i("===", "zzzz");
            FactoryGPS.NaviStart = true;
            factory.StartStopGPS(this, mInfoo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (mNaviMapControl != null) {
            // 坐标转换
            double xy[] = GPSConvert.GEO2PROJECT(Constants.TargetXY[0],
                    Constants.TargetXY[1], mNaviMapControl.getActiveView()
                            .FocusMap().getGeoProjectType());
            // 目标点的空间信息地理坐标
            IPoint tagertpoint = new Point(xy[0], xy[1]);
            // 设置定位图标
            try {
                mNaviMapControl.getActiveView().FocusMap().getGPSContainer()
                        .AddToLocation(tagertpoint);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 设置地图显示范围
        mNaviMapControl.getMap().setExtent(envMapOld);

    }

    void init() {
        // TODO Auto-generated method stub
        mInfoo = (TextView) findViewById(R.id.infoo);
        mNaviMapControl = (MapControl) findViewById(R.id.mapmain);
        mtvInfoDirectory = (TextView) findViewById(R.id.tv_direction);
        mtvInfoDistance = (TextView) findViewById(R.id.tv_distance);
        mtvInfoSpeed = (TextView) findViewById(R.id.tv_speed);
        mivZoomin = (ImageView) findViewById(R.id.zoom_in);
        mivZoomout = (ImageView) findViewById(R.id.zoom_out);
        mivLocationCenter = (ImageView) findViewById(R.id.iv_location_center);
        // 放大
        if (mivZoomin != null) {
            mivZoomin.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            ZoomInCommand zoomin = new ZoomInCommand();
                            zoomin.setBuddyControl(mNaviMapControl);
                            zoomin.setEnable(true);
                            zoomin.onClick(v);
                    }
                    return false;
                }
            });
        }
        // 缩小
        if (mivZoomout != null) {
            mivZoomout.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            ZoomOutCommand zoomout = new ZoomOutCommand();
                            zoomout.setBuddyControl(mNaviMapControl);
                            zoomout.setEnable(true);
                            zoomout.onClick(v);
                    }
                    return false;
                }
            });
        }
        // 定位当前
        if (mivLocationCenter != null) {
            mivLocationCenter.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    CommenUtil.SetLocationCenter(mNaviMapControl);
                    return false;
                }
            });
        }
        setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    goBack();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 返回
     *
     * @throws IOException
     */
    private void goBack() throws IOException {
        if (StringUtil.isFastDoubleClick(500)) {
            return;
        }
        /*
         * 20150626 added by yangzongren 关闭gps的地图导航状态
		 */
        FactoryGPS.NaviStart = false;
        mNaviMapControl.getGPSContainer().RemoveToLocation();
        finish();
    }

    /**
     * 返回键监听事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            try {
                goBack();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
